package com.example.tally;

import android.view.View;
import android.widget.Adapter;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import com.example.tally.adapter.RecordPageAdapter;
import com.example.tally.db.AccountBean;
import com.example.tally.frag_record.IncomeFragment;
import com.example.tally.frag_record.OutcomeFragment;
import com.google.android.material.tabs.TabLayout;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Adler32;

public class RecordActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        // 1、初始化控件
        init();
        // 2、设置ViewPage加载页面
        initPage();
    }

    private void initPage() {
        // 初始化
        List<Fragment> fragmentList = new ArrayList<>();
        // 创建收入和支出页面，放入数据源当中
        OutcomeFragment outcomeFragment = new OutcomeFragment();
        IncomeFragment incomeFragment = new IncomeFragment();
        fragmentList.add(outcomeFragment);
        fragmentList.add(incomeFragment);

        // 创建适配器
        RecordPageAdapter adapter = new RecordPageAdapter(getSupportFragmentManager(), fragmentList);
        // 配置适配器
        viewPager.setAdapter(adapter);
        // 将TabLayout和ViewPage进行关联
        tabLayout.setupWithViewPager(viewPager);

        ArrayList<String> stringList = (ArrayList<String>) getIntent().getStringArrayListExtra("ListString");
        if(stringList != null&&stringList.size()!=0) {
            AccountBean accountBean = new AccountBean();
            accountBean.setId(Integer.parseInt(stringList.get(0)));
            accountBean.setTypeName(stringList.get(1));
            accountBean.setsImageId(Integer.parseInt(stringList.get(2)));
            accountBean.setBeizhu(stringList.get(3));
            accountBean.setMoney(Float.parseFloat(stringList.get(4)));
            accountBean.setTime(stringList.get(5));
            accountBean.setYear(Integer.parseInt(stringList.get(6)));
            accountBean.setMonth(Integer.parseInt(stringList.get(7)));
            accountBean.setDay(Integer.parseInt(stringList.get(8)));
            accountBean.setKind(Integer.parseInt(stringList.get(9)));
            if(accountBean.getKind()==0) {
                outcomeFragment.setAccountBeanBean(accountBean);
                outcomeFragment.setDirection(stringList.get(10));
            }else {
                incomeFragment.setAccountBeanBean(accountBean);
                incomeFragment.setDirection(stringList.get(10));
                tabLayout.getTabAt(1).select();//默认选中第二个
                viewPager.setCurrentItem(1);
            }
        }
    }

    /**
     * 初始化控件
     */
    private void init() {
        tabLayout = findViewById(R.id.record_tabs);
        viewPager = findViewById(R.id.record_vp);
    }

    /**
     * 点击事件
     */
    public void onClick(View v){
        switch (v.getId()){
            case R.id.record_iv_back:
                finish();
                break;
        }
    }
}